<template>
    <div>
        <!-- 面包屑导航区 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>分类参数</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图区 -->
        <el-card>
            <!-- 警告区域 -->
            <el-alert
                show-icon
                title="只允许为第三级分类设置相关参数!"
                type="warning"
                :closable="false">
            </el-alert>
            <!-- 选择商品分类区域 -->
            <el-row class="cat_opt" >
                <el-col>
                    <span>选择商品分类: </span>
                    <!-- 选择商品分类的级联选择框 -->
                    <el-cascader
                        expand-trigger="hover"
                        :options="cateList"
                        :props="cateProps"
                        v-model="selectedCateKeys"
                        @change="handleChange">
                    </el-cascader>
                </el-col>
            </el-row>
            <!-- tab页签区域 -->
            <el-tabs
                v-model="activeName"
                @tab-click="handleClick">
                <!-- 动态参数 -->
                <el-tab-pane label="动态参数" name="many">
                  <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible = true">添加参数</el-button>
                  <!-- 动态参数表格 -->
                  <el-table
                    :data="manyTableData"
                    border
                    stripe
                    style="width: 100%">
                    <!-- 展开行 -->
                    <el-table-column
                      type="expand">
                    </el-table-column>
                    <!-- 索引列 -->
                    <el-table-column
                      type="index">
                    </el-table-column>
                    <el-table-column
                      header-align="center"
                      align="center"
                      prop="attr_name"
                      label="参数名称" >
                    </el-table-column>
                    <el-table-column
                      header-align="center"
                      align="center"
                      label="操作" >
                      <template>
                        <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-tab-pane>
                <!-- 静态属性 -->
                <el-tab-pane label="静态属性" name="only">
                  <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible = true">添加属性</el-button>
                  <!-- 静态属性表格 -->
                  <el-table
                    :data="onlyTableData"
                    border
                    stripe
                    style="width: 100%">
                    <!-- 展开行 -->
                    <el-table-column
                      type="expand">
                    </el-table-column>
                    <!-- 索引列 -->
                    <el-table-column
                      type="index">
                    </el-table-column>
                    <el-table-column
                      header-align="center"
                      align="center"
                      prop="attr_name"
                      label="属性名称" >
                    </el-table-column>
                    <el-table-column
                      header-align="center"
                      align="center"
                      label="操作" >
                      <template>
                        <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-tab-pane>
            </el-tabs>
        </el-card>
        <!-- 添加参数对话框 -->
        <el-dialog
          :title="'添加' + titleText"
          :visible.sync="addDialogVisible"
          width="50%"
          :close="addDialogClosed">
          <!-- 添加参数对话框 -->
          <el-form ref="addFormRef" :model="addForm" label-width="100px" :rules="addFormRules">
            <el-form-item :label="titleText" prop="attr_name">
              <el-input v-model="addForm.attr_name" placeholder="请输入参数名称"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addParams">确 定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script lang="js">
export default {
  data () {
    return {
      // 商品分类列表
      cateList: [],
      // 级联选择框的配置对象
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 级联选择框选中的对象id的数组
      selectedCateKeys: [],
      // 选中的标签
      activeName: 'many',
      // 动态参数数据
      manyTableData: [],
      // 静态属性数据
      onlyTableData: [],
      // 控制添加对话框的显示与隐藏
      addDialogVisible: false,
      // 添加参数的表单数据
      addForm: {
        attr_name: ''
      },
      // 添加表单的验证规则
      addFormRules: {
        attr_name: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      this.cateList = res.data
      console.log(this.cateList)
    },
    // 获取参数的列表数据
    async getParamsData () {
      if (this.selectedCateKeys.length !== 3) {
        this.selectedCateKeys = []
        return
      }
      console.log(this.selectedCateKeys)
      // 根据所选分类的id，和当前所处的面板，获取对应的参数
      const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`, {
        params: { sel: this.activeName }
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      console.log(res.data)
      // 挂载数据
      if (this.activeName === 'many') {
        this.manyTableData = res.data
      } else {
        this.onlyTableData = res.data
      }
    },
    // 级联选择框选中项变化，会触发这个函数
    handleChange () {
      this.getParamsData()
    },
    // tab页签点击事件处理函数
    handleClick () {
      console.log(this.activeName)
      this.getParamsData()
    },
    // 监听添加对话框的关闭操作
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    // 添加参数
    addParams () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return
        }
        const { data: res } = await this.$http.post(`categories/${this.cateId}/attributes`, {
          attr_name: this.addForm.attr_name,
          attr_sel: this.activeName
        })
        if (res.meta.status !== 201) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('添加参数成功')
        this.addDialogVisible = false
        this.getParamsData()
      })
    }
  },
  computed: {
    // 如果按钮需要被禁用则返回true，否则返回false
    isBtnDisabled () {
      if (this.selectedCateKeys.length !== 3) {
        return true
      }
      return false
    },
    // 当前选中的三级分类id
    cateId () {
      if (this.selectedCateKeys.length === 3) {
        return this.selectedCateKeys[2]
      }
      return null
    },
    // 计算对话框标题的方法
    titleText () {
      if (this.activeName === 'many') {
        return '动态参数'
      }
      return '静态属性'
    }
  }
}
</script>

<style lang="less" scoped>
.cat_opt {
    margin: 15px 0;
}
</style>
